/***************************************************************************
创建者：华磊
 开始时间：               : 2020.1１.21
 copyright            : (C) 深圳市华友高科有限公司
 修改说明：（每次有修改就添加一条，带有 修改人，修改时间，修改描述）

修改人:华磊
修改时间:
修改内容:刷新方式,初始化方式等

 ***************************************************************************
 *   消息视窗
*                                                                         *
 ***************************************************************************/
#include "messageview.h"
#include "ui_messageview.h"
#include "hmicommondefine.h"
#include "instructionEnum.h"

//#include <QDebug>

MessageView::MessageView(int styleId,QWidget *parent):
        QDialog(parent),
        ui(new Ui::MessageView)
{
    ui->setupUi(this);

    setGeometry(75,130,460,595);

    initialDisplayStyle(styleId);

    //转移于此
    ui->message_tableWidget->setColumnCount(5);
    ui->message_tableWidget->setColumnWidth(2, 50);
    ui->message_tableWidget->setColumnWidth(3, 70);
    ui->message_tableWidget->setColumnWidth(4, 330);

    tableHeadLable << tr("产生时间") << tr("所属机器人") << tr("级别") << tr("消息ID") << tr("消息内容") ;

    ui->message_tableWidget->setHorizontalHeaderLabels(tableHeadLable);
    ui->message_tableWidget->setSelectionBehavior(QAbstractItemView::SelectRows); //设置选择模式，选择单行
    ui->message_tableWidget->setEditTriggers(QAbstractItemView::NoEditTriggers);

    QString pngError ="/agvwork/AgvHmi/baseBag/hmiProject/resources/messageResources/error.png";
    QString pngWarn ="/agvwork/AgvHmi/baseBag/hmiProject/resources/messageResources/warn.png";
    QString pngNote ="/agvwork/AgvHmi/baseBag/hmiProject/resources/messageResources/note.png";
    QString pngWait ="/agvwork/AgvHmi/baseBag/hmiProject/resources/messageResources/wait.png";
    QPixmap imageError(pngError);
    QPixmap imageWarn(pngWarn);
    QPixmap imageNote(pngNote);
    QPixmap imageWait(pngWait);

    tmpIcon.resize(EM_MESSAGE_LEVEL_END);
    tmpIcon[EM_MESSAGE_LEVEL_ERROR] = QIcon(imageError);   //0
    tmpIcon[EM_MESSAGE_LEVEL_WARN] = QIcon(imageWarn);     //1
    tmpIcon[EM_MESSAGE_LEVEL_NOTE] = QIcon(imageNote);     //2
    tmpIcon[EM_MESSAGE_LEVEL_WAIT] = QIcon(imageWait);     //3

    //初始化选中状态
    errorFlag = 1;
    warnFlag = 1;
    noteFlag = 1;
    waitFlag = 1;
    showType = 1;                           //默认 先显示 全部机器人(2017-2-27改动)

    robotName = "-";

    if(1 == errorFlag)
    {
        ui->error_checkBox->setChecked(true);
    }
    if(1 == warnFlag)
    {
        ui->warn_checkBox->setChecked(true);
    }
   if(1 == noteFlag)
    {
        ui->note_checkBox->setChecked(true);
    }
    if(1 == waitFlag)
    {
        ui->wait_checkBox->setChecked(true);
    }

    if(0 == showType)
    {
        ui->currentRobot_rBtn->setChecked(true);
    }
    else if(1 == showType)
    {
        ui->allRobot_rBtn->setChecked(true);
    }


    Message tmp;
    tmp.MessageLevel = -1;
    tmp.messageType = "";
    tmp.componentClassName = "";
    tmp.MessageInformation = "";
    tmp.messageCode = -1;
    tmp.parameter1 = -1;
    tmp.parameter2 = -1;
    tmp.parameter3 = -1;
    tmp.parameter4 = -1;
    tmp.robotId = -1;
    for(int i = 0;i < MSG_SIZE_MAX;i++)
    {
        selectMessage.append(tmp);//预先初始化 MSG_SIZE_MAX 个位置
    }

    timep = 0;
    p = NULL;

    ui->ClearCurrentMessageBtn->hide();

    QSettings settings(D_SHOW_STYLE_PATH, QSettings::IniFormat);
    int isShowClearMsgServon=  settings.value("Common/isShowClearMsgServon").toInt();
    if(0==isShowClearMsgServon)
    {
        ui->button_clrMsgAndServoOn->hide();
    }
}

MessageView::~MessageView()
{
    delete ui;
}

void MessageView::initialDisplayStyle(int styleId)
{
    switch (styleId) {
    case 1:
        this->setGeometry(0,0,600,450);
        ui->message_tableWidget->setGeometry(10,80,580,310);
        ui->button_clrMsgAndServoOn->setGeometry(20,400,120,40);
        ui->ClearCurrentMessageBtn->setGeometry(330,400,120,40);
        ui->ClearAllMessageBtn->setGeometry(460,400,120,40);
        break;
    default:
        break;
    }
}

/**
 * @brief MessageView::updateView　更新数据到控件上
 */
void MessageView::updateView()
{
    //selectMessage.operator =(message);//未知起危险性

    //qDebug()<<"level = "<<message[0].MessageLevel;
    //qDebug()<<"level = "<<message[1].MessageLevel;
    //qDebug()<<"level = "<<message[2].MessageLevel;

    msgSize = message.size();
    if(msgSize > MSG_SIZE_MAX)
    {
        msgSize = MSG_SIZE_MAX;//目前selectMessage只会显示到30个
    }

    selectSize = 0;
    for(int i = 0; i < msgSize;i++)
    {
        if((message[i].MessageLevel == EM_MESSAGE_LEVEL_ERROR && errorFlag == 1)
            || (message[i].MessageLevel == EM_MESSAGE_LEVEL_WARN && warnFlag == 1)
            || (message[i].MessageLevel == EM_MESSAGE_LEVEL_NOTE && noteFlag == 1)
            || (message[i].MessageLevel == EM_MESSAGE_LEVEL_WAIT && waitFlag == 1)
            )//此处存在疑惑 2020.12.20
        {
            if(0 == showType)
            {
                //if(robotName == str)
                //qDebug()<<"robotID"<<robotID;
                //qDebug()<<"message[i].robotId"<<message[i].robotId;
                if(robotID == message[i].robotId || message[i].robotId < 0)//支持显示无ID的消息显示
                {
                    selectMessage[selectSize].MessageLevel = message[i].MessageLevel;
                    selectMessage[selectSize].robotId = message[i].robotId;//robot name
                    selectMessage[selectSize].time = message[i].time;
                    selectMessage[selectSize].messageCode = message[i].messageCode;
                    selectMessage[selectSize].MessageInformation = message[i].MessageInformation;
                    selectSize++;
                }
            }
            else // == 1
            {
                selectMessage[selectSize].MessageLevel = message[i].MessageLevel;
                selectMessage[selectSize].robotId = message[i].robotId;//robot name
                selectMessage[selectSize].time = message[i].time;
                selectMessage[selectSize].messageCode = message[i].messageCode;
                selectMessage[selectSize].MessageInformation = message[i].MessageInformation;

                //qDebug()<<"level selectMessage = "<<selectMessage[i].MessageLevel;

                selectSize++;
            }
        }
    }

    //qDebug()<<"**msgSize**"<<msgSize;
    //qDebug()<<"**selectSize**"<<selectSize;


    //设置行数
    //if(ui->message_tableWidget->rowCount() != selectSize)
    {
        //ui->message_tableWidget->clear();
        ui->message_tableWidget->setRowCount(selectSize);

        //显示表格内容
        for(int i = 0; i < selectSize; i++)
        {
            if(0 == selectMessage[i].time)      //==0,获取本地
            {
                timep = time(NULL);
            }
            else
            {
                timep = selectMessage[i].time;  //不等于0 arm下补时差
                #ifdef BUILD_ARM
                timep = timep + 28800;          //28800 8小时的秒数
                #endif
            }

            p=localtime(&timep);//从tm结构体中可以取到年月日时分秒等值。
            char szTmp[50] = {0};
            strftime(szTmp,50,"%Y-%m-%d %H:%M:%S",p);    //这里输出的将是 p的时间值

            QString str = QString(QLatin1String(szTmp));
            ui->message_tableWidget->setItem(i, 0, new QTableWidgetItem(str));

            //对应机器人名字
            if(robotID == selectMessage[i].robotId)
            {
                str = robotName;
            }
            else
            {
                //其他机器人
                str = "-";
            }
            ui->message_tableWidget->setItem(i, 1, new QTableWidgetItem(str));

            //icon
            if(selectMessage[i].MessageLevel < EM_MESSAGE_LEVEL_END)
            {
                ui->message_tableWidget->setItem(i, 2, new QTableWidgetItem(tmpIcon[selectMessage[i].MessageLevel] ,""));
            }

            //code
            str = QString::number(selectMessage[i].messageCode);
            ui->message_tableWidget->setItem(i, 3, new QTableWidgetItem(str));

            //messsage
            str = QString::fromStdString(selectMessage[i].MessageInformation);
            ui->message_tableWidget->setItem(i, 4, new QTableWidgetItem(tr(str.toLocal8Bit())));

            //内容适应行高
            ui->message_tableWidget->resizeRowsToContents();
        }
    }
}

void MessageView::setViewText()
{
    tableHeadLable.clear();
    tableHeadLable << tr("产生时间") << tr("所属机器人") << tr("级别") << tr("消息ID") << tr("消息内容") ;
    ui->message_tableWidget->setHorizontalHeaderLabels(tableHeadLable);
}

void MessageView::on_ClearCurrentMessageBtn_clicked()
{
    int messageIndex = ui->message_tableWidget->currentRow();
    emit clearMessage(messageIndex);
}

void MessageView::on_ClearAllMessageBtn_clicked()
{
    emit clearAllMessage();
}

void MessageView::on_error_checkBox_clicked()
{
    emit errorMessageShowOrHide(errorFlag);//1
}

void MessageView::on_warn_checkBox_clicked()
{
    emit warnMessageShowOrHide(warnFlag);
}

void MessageView::on_note_checkBox_clicked()
{
    emit noteMessageShowOrHide(noteFlag);
}

void MessageView::on_wait_checkBox_clicked()
{
    emit waitMessageShowOrHide(waitFlag);
}

void MessageView::on_currentRobot_rBtn_clicked()
{
    emit showCurrentRobotMessage();
}

void MessageView::on_allRobot_rBtn_clicked()
{
    emit showAllRobotMessage();
}

void MessageView::changeLanguageSlot(int language)
{
    if(1 == language)
    {
        translator.load("/agvwork/AgvHmi/baseBag/hmiProject/language/messageViewEnglishLan.qm");
        qApp->installTranslator(&translator);
    }
    else
    {
        qApp->removeTranslator(&translator);
    }
    ui->retranslateUi(this);

    setViewText();
}


void MessageView::on_button_clrMsgAndServoOn_clicked()
{
    emit clearMsgAndServoOn_signal();
}
